/* 
 * ***** BEGIN LICENSE BLOCK ***** Version: NPL 1.1/GPL 2.0/LGPL 2.1 The
 * contents of this file are subject to the Netscape Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.mozilla.org/NPL/
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License. The
 * Original Code is lutsko.com code. The Initial Developer of the Original Code
 * is James Lutsko. Portions created by the Initial Developer are Copyright (C)
 * 2003 the Initial Developer. All Rights Reserved. Contributor(s):
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or the
 * GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
 * case the provisions of the GPL or the LGPL are applicable instead of those
 * above. If you wish to allow use of your version of this file only under the
 * terms of either the GPL or the LGPL, and not to allow others to use your
 * version of this file under the terms of the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice and other
 * provisions required by the GPL or the LGPL. If you do not delete the
 * provisions above, a recipient may use your version of this file under the
 * terms of any one of the NPL, the GPL or the LGPL. ***** END LICENSE BLOCK
 * *****
 */

package autoteam.tables;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import autoteam.Child;
import autoteam.League;
import autoteam.Local;
import autoteam.PlayerData;
import autoteam.ui.SkillEditor;
import autoteam.ui.dialogs.ChildEditor;
import autoteam.ui.dialogs.WeightEditorDialog;
import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;

public class KidsTableModel extends AutoTeamTableModel
{
    private static final long serialVersionUID = 8142486728969801481L;
    private DataAccessObject dao;
    private List<DBRecord> kidRecords;
    private PlayerData player;
    
    League doc = null;
    final Boolean t = new Boolean( true );
    final Boolean f = new Boolean( false );
    final Double d1 = new Double( 1.0 );
    final Integer d2 = new Integer( 1 );
    final String[] columnNames = { "Last Name", "First Name", "Total Skill", 
				   "Monday", "Tuesday", "Wednesday",
				   "Thursday", "Friday", "Comments" 
				 };
    
    public KidsTableModel( League newLeague )
    {
    	doc = newLeague;
    	dao = new DataAccessObject();
    	kidRecords = dao.getAllRecords("players");
    }
    
    public int getColumnCount( )
    {
    	return columnNames.length;
    }
    
    public int getRowCount( )
    {
	return doc.getNumberOfChildren( );
    }
    
    public void add( int j )
    {
		Child c = (Child) doc.newChild( );
		if ( ChildEditor.showDialog( null, c ) )
		    fireTableDataChanged( );
		if ( SkillEditor.showDialog( null, c, doc ) )
		    fireTableDataChanged( );
		if ( WeightEditorDialog.showDialog( null, doc ) )
		    fireTableDataChanged( );
		player.addPlayer(c);
    }
    
    public void delete( int r )
    {
    	doc.removeChild( r );
    }
    
    public String getColumnName( int col )
    {
	return columnNames[col];
    }
    
    public Object getValueAt( int row, int col )
    {
	Child c = (Child) doc.getChild( row );
	if ( col == 0 )
	    return c.lastName;
	if ( col == 1 )
	    return c.firstName;
	if ( col == 2 )
	    return Local.format( c.totalSkill );
	if ( col >= 3 && col <= 7 )
	    return c.getBaseNight( col - 3 );
	return c.comment;
    }
    
    /*
     * JTable uses this method to determine the default renderer/ editor for
     * each cell. If we didn't implement this method, then the last column would
     * contain text ("true"/"false"), rather than a check box.
     */
    public Class getColumnClass( int c )
    {
	if ( c < 3 || c == 8 )
	    return getColumnName( 2 ).getClass( );
	return t.getClass( );
    }
    
    /*
     * Don't need to implement this method unless your table's editable.
     */
    public boolean isCellEditable( int row, int col )
    {
	return true;
    }
    
    public void setValueAt( Object value, int row, int col )
    {
	if ( row >= doc.getNumberOfChildren( ) )
	    return;
	
	Child c1 = (Child) doc.getChild( row );
	
	if ( col == 0 )
	{
	    doc.appendToLog( "Last name of " + c1.toString( ) + " changed to " + value.toString( ) );
	    c1.lastName = value.toString( );
	    ColumnData[] identifiers = { new ColumnData("playerId",c1.id) };
	    ColumnData[] changes = { new ColumnData("lastname",value.toString()) };
	    dao.updateRecord("players", identifiers, changes);
	}
	else if ( col == 1 )
	{
	    doc.appendToLog( "First name of " + c1.toString( ) + " changed to " + value.toString( ) );
	    c1.firstName = value.toString( );
	    ColumnData[] identifiers = { new ColumnData("playerId",c1.id) };
	    ColumnData[] changes = { new ColumnData("firstname",value.toString()) };
	    dao.updateRecord("players", identifiers, changes);
	}
	if ( col == 2 )
	{
	    doc.appendToLog( "Total skill of " + c1.toString( ) + " changed to " + value.toString( ) );
	    c1.totalSkill = Double.parseDouble( (String) value );
	    ColumnData[] identifiers = { new ColumnData("playerId",c1.id) };
	    ColumnData[] changes = { new ColumnData("lastname",value.toString()) };
	    dao.updateRecord("players", identifiers, changes);
	}
	else
	{
	    doc.appendToLog( "Changed night " + (col - 3) + " of " + c1.toString( ) + " to " + value.toString( ) );
	    if ( col >= 3 && col <= 7 )
	    {
	    	boolean theValue = ((Boolean) value).booleanValue() ? Boolean.TRUE : Boolean.FALSE;
			c1.setBaseNight(col-3,theValue);
			c1.setTransientNight(col-3,theValue);
			ColumnData[] identifiers = { new ColumnData("playerId",c1.id) };
			Map<Integer,String> intToNightMap = new HashMap<Integer,String>();
			intToNightMap.put(0, "monday");
			intToNightMap.put(1, "tuesday");
			intToNightMap.put(2, "wednesday");
			intToNightMap.put(3, "thursday");
			intToNightMap.put(4, "friday");
			int booleanToIntValue = 0;
			if (theValue == true)
				booleanToIntValue = 1;
			ColumnData[] changes = { new ColumnData(intToNightMap.get(col-3),booleanToIntValue) };
			dao.updateRecord("players", identifiers, changes);
	    }
	}
    }
    
}
